package c.a.md.androidvideo.gles;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by ytf on 2018/10/30 030.
 * Description:
 */
public class PointRender implements GLSurfaceView.Renderer
{
    private static final String VERTEX_RENDER =
            "void main(){"
            + "gl_Position = vec4(0.0, 0.0, 0.0, 1.0);"
            + "gl_PointSize = 20.0;"
            + "}";

    private static final String FRAGMENT_RENDER =
            "void main(){"
            + "gl_FragColor = vec4(1., 1., 0.0, 1.0);"
            + "}";

    private int mGLProgram = -1;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        GLES20.glClearColor(0, 0, 0, 1);
        int vsh = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
        GLES20.glShaderSource(vsh, VERTEX_RENDER);
        GLES20.glCompileShader(vsh);

        int fsh = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
        GLES20.glShaderSource(fsh, FRAGMENT_RENDER);
        GLES20.glCompileShader(fsh);

        mGLProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mGLProgram, vsh);
        GLES20.glAttachShader(mGLProgram, fsh);
        GLES20.glLinkProgram(mGLProgram);
        GLES20.glValidateProgram(mGLProgram);

        int statues[] = new int[1];
        GLES20.glGetProgramiv(mGLProgram, GLES20.GL_VALIDATE_STATUS, statues, 0);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl)
    {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glUseProgram(mGLProgram);
        GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
    }
}
